public class Solution {
    public static String truncateSentence(String s, int k) {
        String[] s1 = new String[k];
        StringBuilder ret = new StringBuilder("");
        s1 = s.split(" ");
        for (int i = 0; i < k - 1; i++) {
            ret.append(s1[i]);
        }
        ret.append(s1[k - 1]);
        return String.valueOf(ret);

    }

    public static int removeDuplicates(int[] nums) {
        int idx = 0;
        int i = 0;
        for (i = 0; i < nums.length; i++) {
            if (i < nums.length - 1 && nums[i] == nums[i + 1]) {
                continue;
            } else {
                nums[idx++] = nums[i];
            }
        }
        return idx;
    }

    public static int into(char ch) {
        switch (ch) {
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
            case 'a':
                return 4;
            case 'b':
                return 9;
            case 'c':
                return 40;
            case 'd':
                return 90;
            case 'e':
                return 400;
            case 'f':
                return 900;
        }
        return 0;
    }

    public static int romanToInt(String s) {
        s = s.replace("IV", "a");
        s = s.replace("IX", "b");
        s = s.replace("XL", "c");
        s = s.replace("XC", "d");
        s = s.replace("CD", "e");
        s = s.replace("CM", "f");
        int ret = 0;
        for (int i = 0; i < s.length(); i++) {
            ret += into(s.charAt(i));
        }
        return ret;
    }
    public static int  SquareNum(int n){
        int ret=0;
        while(n>0){
            ret+=(n%10)*(n%10);
            n=n/10;
        }
        return ret;
    }
    public static boolean isHappy(int n) {
        int fast=n;
        int slow=n;
        do{
            fast=SquareNum(fast);
            fast=SquareNum(fast);
            slow=SquareNum(slow);
        }while(fast!=slow);
        if(fast==1){
            return true;
        }else{
            return false;
        }
    }
    public static void main2(String[] args) {
        //String s="hello how are you contestant";
        //String ret=truncateSentence(s,4);
        //System.out.println(ret);
        //System.out.println(14^3);
        /*String s1="MCMXCIV";
        System.out.println(romanToInt(s1));*/
    }

    public static void main(String[] args) {
       // System.out.println(SquareNum(12));
        System.out.println(isHappy(19));

    }
}
